<template>
  <el-container class="bigbox">
    <el-container>
      <el-aside style="position: relative;"></el-aside>
      <el-main>
        <div class="formBox">
          <div class="toggle-div">
            <span class="toggle-btn" :class="{ 'active': isLogin }" @click="toggleView(true)">登录</span>
            <span class="line">|</span>
            <span class="toggle-btn" :class="{ 'active': !isLogin }" @click="toggleView(false)">注册</span>
          </div>
          <el-form v-if="isLogin" ref="loginForm" :model="loginUser" label-width="100px" :rules="loginRules">
            <el-form-item label="账号" prop="username">
              <el-input v-model="loginUser.username" placeholder="4到16位(字母,数字,下划线,减号)"/>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="loginUser.password" placeholder="6到16位数字"/>
            </el-form-item>
            <el-form-item>
              <el-button class="login-btn" type="primary" @click="login">登录</el-button>
            </el-form-item>
          </el-form>
          <el-form v-else ref="signupForm" :model="signupUser" label-width="100px" :rules="signupRules">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="signupUser.username" placeholder="4到16位(字母,数字,下划线,减号)"/>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="signupUser.password" placeholder="6到18位数字"/>
            </el-form-item>
            <el-form-item label="确认密码" prop="passwordAgain">
              <el-input type="password" v-model="signupUser.passwordAgain" placeholder="请再次输入密码"/>
            </el-form-item>
            <el-form-item>
              <el-button class="signup-btn" type="primary" @click="signup">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { doGet, doPost } from "../../http/httpUserRequest.js";
import { getTokenName, messageTip, removeToken } from "../../util/util.js";

const isLogin = ref(true);
const loginUser = ref({ username: '', password: '' });
const signupUser = ref({ username: '', password: '', passwordAgain: '' });

const loginRules = {
  username: [{ required: true, message: '请输入登录账号', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入登录密码', trigger: 'blur' },
    { min: 6, max: 16, message: '登录密码长度为6-16位', trigger: 'blur' }
  ],
};

const signupRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 16, message: '用户名长度应在4到16个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '密码长度应在6到18个字符之间', trigger: 'blur' }
  ],
  passwordAgain: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: checkPasswordAgain, trigger: 'blur' }
  ],
};

function checkPasswordAgain(rule, value, callback) {
  if (value !== signupUser.value.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
}

function toggleView(isLoginView) {
  isLogin.value = isLoginView;
}

function login() {
  const form = ref(null);
  form.value.validate((isValid) => {
    if (isValid) {
      const formData = new FormData();
      formData.append("username", loginUser.value.username);
      formData.append("password", loginUser.value.password);
      doPost("/user/login", formData).then((resp) => {
        if (resp.data.code === 200) {
          messageTip("登录成功", "success");
          removeToken();
          window.location.href = "/dashboard";
        } else {
          messageTip("登录失败", "error");
        }
      });
    }
  });
}

function signup() {
  const form = ref(null);
  form.value.validate((isValid) => {
    if (isValid) {
      const formData = new FormData();
      formData.append("username", signupUser.value.username);
      formData.append("password", signupUser.value.password);
      doPost("/user/signup", formData).then((resp) => {
        if (resp.data.code === 200) {
          messageTip("注册成功", "success");
          window.location.href = "/login";
        } else {
          messageTip("注册失败", "error");
        }
      });
    }
  });
}

onMounted(() => {
  freeLogin();
});

function freeLogin() {
  const token = window.localStorage.getItem(getTokenName());
  if (token) {
    doGet("/user/login/free", {}).then((resp) => {
      if (resp.data.code === 200) {
        window.location.href = "/dashboard";
      }
    });
  }
}
</script>

<style scoped>
.formBox {
  border: 1px solid grey;
  box-shadow: 0px 0px 10px 1px grey;
  border-radius: 4px;
  width: 441px;
  height: 303px;
  margin: auto;
  margin-top: 20%;
}
.toggle-div {
  margin-bottom: 30px;
  margin-top: 30px;
  margin-left: 50px;
}
.toggle-div .line {
  font-size: 20px;
}
.toggle-div .toggle-btn {
  font-size: 25px;
  font-weight: bold;
  cursor: pointer;
}
.toggle-div .toggle-btn:hover,
.toggle-div .toggle-btn.active {
  color: #393939;
}
.toggle-div .toggle-btn.active {
  border-bottom: 6px solid skyblue;
}
.el-input {
  width: 80%;
}
.el-aside {
  background: #1a1a1a;
  width: 50%;
  background: url(../../image/1.jpg);
  background-repeat: no-repeat;
  background-size: cover;
}
.el-main {
  height: 100%;
}
.login-btn, .signup-btn {
  width: 100px;
  margin-right: 20px;
}
</style>